package com.capri.activity;


import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.capri.adapter.OrderListAdapter;
import com.capri.R;
import com.capri.model.INurse;
import com.capri.model.bean.AllOrdersBean;
import com.capri.model.bean.BaseResultBean;
import com.capri.model.impl.NurseModel;
import com.capri.utils.CommonSubscriber;

import cc.cloudist.acplibrary.ACProgressConstant;
import cc.cloudist.acplibrary.ACProgressFlower;
import es.dmoral.toasty.Toasty;
import rx.functions.Action0;

/**
 * 全部订单列表
 */
public class OrderListFragment extends Fragment {
    private static final String ORDERTYPE = "OrderType";
    private String OrderType;

    private boolean isViewCreated = false;
    private boolean isInitialedData = false;
    private boolean isVisible = false;

    private ListView list_orders;
    private OrderListAdapter adapter;
    private INurse model;
    ACProgressFlower dialog;


    public OrderListFragment() {
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param orderType Parameter 1.
     * @return A new instance of fragment OrderListFragment.
     */
    public static OrderListFragment newInstance(String orderType) {
        OrderListFragment fragment = new OrderListFragment();
        Bundle args = new Bundle();
        args.putString(ORDERTYPE, orderType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            OrderType = getArguments().getString(ORDERTYPE);
            adapter = new OrderListAdapter(getContext());
        }
        initDialog();
    }

    private void initDialog() {
        dialog = new ACProgressFlower.Builder(getContext()).bgAlpha(0)
                .direction(ACProgressConstant.DIRECT_CLOCKWISE)
                .themeColor(Color.WHITE)
                .fadeColor(Color.TRANSPARENT).build();
        dialog.setCanceledOnTouchOutside(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        isViewCreated = true;
        lazyData();
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        list_orders = view.findViewById(R.id.list_orders);
        list_orders.setAdapter(adapter);
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = true;
        lazyData();
    }

    private void lazyData() {
        if ((!isInitialedData) && (isVisible) && isViewCreated) {
            isInitialedData = true;
            String serviceStatus = "";
            if ("all".equals(OrderType)) {
                serviceStatus = "";//全部
            } else if ("waitConfirm".equals(OrderType)) {
                serviceStatus = "waitConfirm";//待接单
            } else if ("waitStartOff".equals(OrderType)) {
                serviceStatus = "waitStartOff";//待出诊
            } else if ("startOff".equals(OrderType)) {
                serviceStatus = "startOff";//已出诊
            } else if ("arrived".equals(OrderType)) {
                serviceStatus = "arrived";//已到达
            } else if ("startService".equals(OrderType)) {
                serviceStatus = "startService";//已到达
            } else if ("serviceFinish".equals(OrderType)) {
                serviceStatus = "serviceFinish";//已结束
            }
            if (model == null) {
                model = new NurseModel();
            }
            model.GetAllOrders(serviceStatus).doOnSubscribe(new Action0() {
                @Override
                public void call() {
                    dialog.show();
                }
            }).subscribe(new CommonSubscriber<AllOrdersBean>() {
                @Override
                public void onSuccess(AllOrdersBean response) {
                    dialog.dismiss();
                    adapter.setDatas(response.getList());
                }

                @Override
                public void onFailure(BaseResultBean response) {
                    dialog.dismiss();
                    Toasty.error(getActivity(), response.getMsg(), Toast.LENGTH_SHORT, true).show();
                }
            });
        }
    }
}
